/**
 * Created by Administrator on 2018-07-10.
 */

define(function (require,exports,module) {
    var sTpl = require("./order.html");
    var VueComponent = Vue.extend({
        props:['orderParentData'],
        template: sTpl,
        data:function () {
            return {
                ifOrderList:false,
                title:'保存成功',
                title1:'保存失败',
                suppliers:[],
                pickerOptions1: {
                    shortcuts: [{
                        text: '今天',
                        onClick:function(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick:function(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick:function (picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                pickerOptions2: {
                    shortcuts: [{
                        text: '今天',
                        onClick:function(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick:function(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick:function (picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                orderFormInline:{
                    auditStatus:'未审核',
                    totalMoney:0.0,
                    purchaseDate:'',
                    supplier:'',
                    contacts:'',
                    phone:'',
                    interactiveAddress:'',
                    interactiveDate:'',
                    buyGoodsPos:[]
                },
                addNeedGoodsFormInline:{
                    purchasePlanNo:'',
                    goodsCode:'',
                    goodsName:'',
                    goodsSpec:'',
                    needNum:'',
                    unit:'',
                    budgetPrice:'',
                    budgetMoney:''
                },
                dialogFormVisible:false,
                purchasePlanNo:'',
                purchasePlans:[],
                purchasePlans1:[],
                ifEdit:false,
                isShow:false,
                isEditOrAdd:true,
                index:''
            }

        },
        methods:{
            findAllSupplierMgrPo:function() {
                var $this = this;
                axios.get(basepath+"/api/inventoryManagement/findAllSupplierMgrPo"
                ).then(function (response) {
                    $this.suppliers=response.data;
                });
            },
            getPurchasePlan:function () {
                    var $this = this;
                    axios.get(basepath+"/api/purchaseMgr/getPurchasePlanPoByState"
                    ).then(function (response) {
                        console.log(response);
                        $this.purchasePlans=response.data;
                    });

            },
            addNeedGoods:function () {
                this.ifEdit=false;
                this.dialogFormVisible=true;
                this.addNeedGoodsFormInline={
                    purchasePlanNo:'',
                        goodsCode:'',
                        goodsName:'',
                        goodsSpec:'',
                        purchaseNum:'',
                        unit:'',
                        budgetPrice:'',
                        budgetMoney:''
                };
                var flag;
                var $this=this.orderFormInline.buyGoodsPos;
                this.purchasePlans1=[];
                this.purchasePlans1=this.purchasePlans.filter(function(data){
                   if($this==''){
                       return data;
                   }
                    flag=false;
                    for(var j=0;j<$this.length;j++){
                        if(data.purchasePlanNo==$this[j].purchasePlanNo){
                            flag=true;
                            return;
                        }
                    }
                    if(!flag){
                        return data;
                    }
                });
            },
            clear:function () {
                this.dialogFormVisible=false;
            },
            addGoods:function () {
                if(!this.ifEdit){
                    this.orderFormInline.buyGoodsPos.push(this.addNeedGoodsFormInline);
                }
                this.dialogFormVisible=false;
            },
            purchasePlanChange:function (value) {
                if(!value){
                    this.addNeedGoodsFormInline={
                        projectName:'',
                        purchasePlanNo:'',
                        goodsCode:'',
                        goodsName:'',
                        goodsSpec:'',
                        purchaseNum:'',
                        unit:'',
                        budgetPrice:'',
                        budgetMoney:''
                };
                }else{
                    this.addNeedGoodsFormInline=this.purchasePlans.filter(function (data) {
                        if(data.purchasePlanNo == value){
                            return data;
                        }
                    })[0];
                }
            },
            editNeedGoods:function (scope) {
                this.ifEdit=true;
                this.index=scope.$index;
                this.addNeedGoodsFormInline=scope.row;
                this.dialogFormVisible=true;
            },
            deleteNeedGoods:function (scope) {
                alert(scope.$index);
                this.orderFormInline.buyGoodsPos.splice(scope.$index,1);
            },
            saveOrder:function () {
                this.orderFormInline.totalMoney=this.totalMoney;
                var $this = this;

                this.$refs.orderFormInline.validate(function(validate){
                    if(validate){
                        axios.post(basepath+'/api/purchaseMgr/saveBuyOrderPo',$this.orderFormInline)
                            .then(function (response) {
                                $this.$refs["orderFormInline"].resetFields();
                                $this.$notify({
                                    title: $this.title,
                                    message: $this.title
                                });
                            });
                        $this.orderParentData.returnOrderParent();
                    }else{
                        $this.$notify({
                            title: $this.title1,
                            message: $this.title1
                        });
                        return  false;
                    }

                });




            },
            closeOrder:function () {
                this.orderParentData.returnOrderParent();
            },
            clearOrder:function () {
                this.orderParentData.returnOrderParent();
            }

        },
        watch:{



        },
        computed:{
            totalMoney:function () {
                var total=0.00;
                for(var i=0;i<this.orderFormInline.buyGoodsPos.length;i++){
                    total=total+parseFloat(this.orderFormInline.buyGoodsPos[i].budgetMoney);
                }
                return total;
            }
        },
        created:function () {
            this. findAllSupplierMgrPo();
            this.getPurchasePlan();

        },
        activated:function () {
            messages.$emit('change','采购订单');
        }

    })
    module.exports=VueComponent;
    module.exports.reDefine=function (val) {
        module.exports.type=val;
        return module.exports;
    }

})

